'use strict';
app.controller('industryCtrl',['$scope','industryService','toaster', function($scope,industryService,toaster){
    industryService.getIndustryList($scope).then(function(result){
        switch (result.status){
            case 200:
                $scope.data = [];
                $scope.data = result.data;
                break;
            default :
                toaster.pop('error', "操作失败", "发生未知错误！请联系管理员");
                break
        }
    });

}]);


app.controller('createIndustryCtrl',['$scope','industryService','$state','toaster',function($scope,industryService,$state,toaster){
    $scope.createIndustry = function(){
        industryService.addIndustry($scope).then(function(result){
            switch (result.status){
                case 200:
                    $scope.cancel();
                    $state.reload('app.industry');
                    toaster.pop('success', "提示", "操作成功");
                    break;
                case 604:
                    toaster.pop('waiting', "操作失败", result.message);
                    break;
                default :
                    toaster.pop('error', "操作失败", "发生未知错误！请联系管理员");
                    break
            }
        });
    }
}]);

app.controller('industrySelectCtrl', ['$scope', '$modalInstance', 'items', function($scope, $modalInstance, items) {
    $scope.items = items;
    $scope.selected = {
        item: $scope.items[0]
    };

    $scope.cancel = function () {
        $modalInstance.dismiss('close');
    };
}])
;
app.controller('getIndustryCtrl', ['$scope', '$modal', '$log', 'industryService','toaster',function($scope, $modal, $log,industryService,toaster) {
    $scope.open = function (size) {
        var modalInstance = $modal.open({
            templateUrl: 'industryAdd.html',
            controller: 'industrySelectCtrl',
            size: size,
            scope:$scope,
            resolve: {
                items: function () {
                    var data = industryService.getIndustryFullName($scope).then(function(result){
                        var industryType = [];
                        switch (result.status){
                            case 200:
                                industryType = result.data;
                                break;
                            default :
                                toaster.pop('error', "操作失败", "发生未知错误！请联系管理员");
                                break
                        }
                        return industryType;
                    });
                    return data;
                }
            }
        });
    };
}]);


app.controller('industryDel', ['$scope', '$modalInstance', 'items','industryService','$state','toaster',
    function($scope, $modalInstance, items,industryService,$state,toaster) {
    $scope.items = items;
    $scope.selected = {
        item: $scope.items[0]
    };
    $scope.industryDelete = function (id) {
        industryService.delIndustry($scope,id).then(function(result){
            switch(result.status){
                case 200:
                    toaster.pop('success', "提示", "删除成功");
                    $state.reload('app.industry');
                    $scope.cancel();
                    break;
                case 735:
                    toaster.pop('error', "错误提示", "该行业下仍存在商家，请删除后再删除该行业！");
                    $scope.cancel();
                    break;
                default :
                    toaster.pop('error', "操作失败", "发生未知错误！请联系管理员");
                    break;
            }
        });
    };


    $scope.cancel = function () {
        $modalInstance.dismiss('cancel');
    };
}])
;
app.controller('ModalDemoCtrl', ['$scope', '$modal', '$log', function($scope, $modal, $log) {
    $scope.delete = function (id) {
        var modalInstance = $modal.open({
            templateUrl: 'industryDel.html',
            controller: 'industryDel',
            scope:$scope,
            resolve: {
                items: function () {
                    return id;
                }
            }
        });

        modalInstance.result.then(function (selectedItem) {
            $scope.selected = selectedItem;
        }, function () {
            $log.info('Modal dismissed at: ' + new Date());
        });
    };

    $scope.edit = function (id,money,name,parentId) {
        var modalInstance = $modal.open({
            templateUrl: 'industryEdit.html',
            controller: 'industryDel',
            scope:$scope,
            resolve: {
                items: function () {
                    var data={};
                    data.id=id;
                    data.name=name;
                    data.money=money;
                    data.parentId = parentId;
                    return data;
                }
            }
        });
        modalInstance.result.then(function (selectedItem) {
            $scope.selected = selectedItem;
        }, function () {
            $log.info('Modal dismissed at: ' + new Date());
        });
    };
}]);

app.controller('editIndustryCtrl',['$scope','industryService','$state','toaster',function($scope,industryService,$state,toaster){
    $scope.editIndustry = function(){
        industryService.updateIndustry($scope).then(function(result){
            switch (result.status){
                case 200:
                    $scope.cancel();
                    toaster.pop('success', "提示", "操作成功");
                    $state.reload('app.industry');
                    break;
                case 604:
                    toaster.pop('error', "操作失败", "行业名称已存在");
                    break;
                default :
                    toaster.pop('error', "操作失败", "发生未知错误！请联系管理员");
                    break
            }
        });
    }
}]);

